package com.meirengu.webview.service.impl;

import com.meirengu.common.StatusCode;
import com.meirengu.model.Result;
import com.meirengu.utils.HttpUtil;
import com.meirengu.utils.HttpUtil.HttpResult;
import com.meirengu.webview.dao.CheckCodeDao;
import com.meirengu.webview.model.CheckCode;
import com.meirengu.webview.service.CheckCodeService;
import com.meirengu.webview.service.SmsService;
import com.meirengu.webview.utils.ConfigUtil;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 验证码服务实现类
 *
 * @author Marvin
 * @create 2017-01-12 下午7:24
 */
@Service
public class CheckCodeServiceImpl implements CheckCodeService {

    private static final Logger logger = LoggerFactory.getLogger(CheckCodeServiceImpl.class);

    @Autowired
    CheckCodeDao checkCodeDao;

    @Autowired
    private SmsService smsService;

    @Override
    public int generate() {
        return RandomUtils.nextInt(100000, 999999);
    }

    @Override
    public HttpResult send(String mobile, int code, String ip, String type) {
        HttpResult hr = null;
        Map<String, String> params = new HashMap<String, String>();
//        params.put("tpl_id", type);
        params.put("mobile", mobile);
        params.put("ip", ip);
//        params.put("code", code+"");
        String text = "【大额推广】您的验证码是：" + code;

        params.put("text", text);
        String url = ConfigUtil.getConfig("URI_CHECKCODE_SEND");
        logger.info("CheckCodeServiceImpl.send post >> uri :{}, params:{}", new Object[]{url, params});
        try {
            hr = HttpUtil.doPostForm(url, params);
        } catch (Exception e) {
            logger.error("CheckCodeServiceImpl.send error >> params:{}, exception:{}", new Object[]{params, e});
        }
        return hr;
    }

    @Override
    public int create(CheckCode checkCode) {
        return checkCodeDao.create(checkCode);
    }

    @Override
    public int update(CheckCode checkCode) {
        return checkCodeDao.update(checkCode);
    }

    @Override
    public CheckCode retrieve(String mobile, int code) {
        Map params = new HashMap();
        params.put("mobile", mobile);
        params.put("code", code);
        return checkCodeDao.retrieve(params);
    }

    public Result singleSend(String mobile, int code, String ip, String type) throws Exception {
        Result result = new Result();
        String text = "【卡王贷】您的验证码是：" + code;
        logger.info("SmsServiceImpl.singleSend params >> mobile:{}, text:{}", new Object[]{mobile, text});
        String domain = ConfigUtil.getConfig("SMS_DOMAIN");
        String uid = ConfigUtil.getConfig("SMS_UID");
        String pw = ConfigUtil.getConfig("SMS_PW");
        String PostData = "uid=" + uid + "&pw=" + pw + "&mb=" + mobile + "&ms=" + java.net.URLEncoder.encode(text, "utf-8");
        String ret = smsService.SMS(PostData, "http://" + domain + "/send.do");

        if (StringUtils.isNotBlank(ret)) {
            if (ret.contains(",")) {
                String[] retarr = ret.split(",");
                code = Integer.parseInt(retarr[0]);
                logger.info(retarr[1]);
                if (code == 0) {
                    result.setCode(StatusCode.OK);
                    result.setMsg(StatusCode.codeMsgMap.get(StatusCode.OK));
                }
            }

        }
        return result;
    }

    @Override
    public int getCount(Map map) throws Exception {
        return checkCodeDao.getCount(map);
    }

    @Override
    public List<Map<String, Object>> getMobileCount(Map map) throws Exception {
        return checkCodeDao.getMobileCount(map);
    }


}
